<?php
/**
 * Created by PhpStorm.
 * User: qixiuxiu
 * Date: 2017/4/5
 * Time: 9:54
 */
/*不同于使用一个URL映射数组，Routing组件依赖一个RoutingCollection实例*/
use Symfony\Component\Routing\RouteCollection;
/*添加路由*/
use Symfony\Component\Routing\Route;
/*一个UrlMatcher实例能够匹配URL路径*/
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;

class rout{
    function routcollectionAction(){
        $routes = new RouteCollection();

        $routes->add('hello',new Route('/hello/{name}',array('name' => 'world')));
        $routes->add('bye',new Route('/bye'));

        $context = new RequestContext();
        $context->fromRequest($request);
        $matcher = new UrlMatcher($routes, $context);

        $attributes = $matcher->match($request->getPathInfo());
        print_r($matcher->match('/bye'));
        /* Gives:
        array (
          '_route' => 'bye',
        );
        */

        print_r($matcher->match('/hello/Fabien'));
        /* Gives:
        array (
          'name' => 'Fabien',
          '_route' => 'hello',
        );
        */

        print_r($matcher->match('/hello'));
        /* Gives:
        array (
          'name' => 'World',
          '_route' => 'hello',
        );
        */
        $matcher->match('/not-found');
        // throws a Symfony\Component\Routing\Exception\ResourceNotFoundException
    }
}



